package com.NeuglsWorkStudio.Utility{  
	
	import flash.events.NetStatusEvent;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	
	
	
	/**   
	 * @author Neugls 
	 * @version 0.5 
	 * @time 7/21/2011
	 */    
	public class CookieManager {  
		
		private var _FTime:uint;  
		private var _FName:String;  
		private var _FSO:SharedObject;  
		private var _FOnError:Function=null;
		public static var KeyPrefix:String='3dpageflip_';
		/**
		 * 创建CookieManager
		 * @param name Cookie Name
		 * @param timeOut 
		 * @param OnError this function may be called when error occoured,need one param ErrorCode:int,
		 * The value of ErrorCode can be accquired from CookieManager.
		 * */
		public function CookieManager(name:String = "Cookie", timeOut:uint=3600,onError:Function=null) {  
			_FName = name;  
			_FTime = timeOut;  
			_FOnError=onError;
			try{
				_FSO = SharedObject.getLocal(name, "/");
			}catch(E:Error){
				DisplayError(ecSharedObjectGetLocalError);				
			}
		}  
		
		public static const ecSharedObjectGetLocalError:int=1;
		public static const ecSharedObjectFlushDataError:int=2;
		public static const ecSharedObjectUserDenyStore:int=3;
		
		
		private function DisplayError(MsgID:int):void{
			if(_FOnError!=null){
				_FOnError(MsgID);
			}
		}
		
		private function TryFlushSO():void{
			var R:String;
			try{
				R=_FSO.flush();
			}catch(E:Error){
				DisplayError(ecSharedObjectFlushDataError);
			}
			switch(R){
				case SharedObjectFlushStatus.FLUSHED:
					break;
				case SharedObjectFlushStatus.PENDING:
					_FSO.addEventListener(NetStatusEvent.NET_STATUS,onFlushStatus);
					break;
			}
		}
		
		private function onFlushStatus(E:NetStatusEvent):void{
			switch (E.info.code) {
				case "SharedObject.Flush.Success":
					break;
				case "SharedObject.Flush.Failed":
					DisplayError(ecSharedObjectUserDenyStore);
					break;
			}
			_FSO.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
		}
		
		/**
		 * 清除超时内容
		 * */  
		public function clearTimeOut():void {  
			if(!_FSO) return;
			
			var obj:* = _FSO.data.cookie;  
			if(obj == undefined){  
				return;  
			}  
			for(var key:* in obj){  
				if(obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)){  
					delete obj[key];  
				}  
			}  
			_FSO.data.cookie = obj;  
			TryFlushSO();
		}  
		
		private function isTimeOut(time:uint):Boolean {  
			if(!_FSO) return true;
			
			var today:Date = new Date();          
			return time + _FTime * 1000 < today.getTime();  
		}  
		
		//获取超时值;  
		public function getTimeOut():uint {  
			return _FTime;  
		}  
		
		//获取名称;  
		public function getName():String {  
			return _FName;  
		}  
		
		//清除Cookie所有值;  
		public function clear():void {  
			if(!_FSO) return;
			
			_FSO.clear();  
		}  
		
		//添加Cookie值  
		public function put(key:String, value:*):void { 
			if(!_FSO) return;
			
			var today:Date = new Date();  
			key = KeyPrefix+key;  
			value.time = today.getTime();  
			if(_FSO.data.cookie == undefined){  
				var obj:Object = {};  
				obj[key] = value;  
				_FSO.data.cookie = obj;  
			}else{  
				_FSO.data.cookie[key] = value;  
			}  
			TryFlushSO();
		}  
		
		
		/**
		 * 删除Cookie值;
		 * */  
		public function remove(key:String):void {  
			if(!_FSO) return;
			
			if (contains(key)) {  
				delete _FSO.data.cookie[KeyPrefix + key];  
				TryFlushSO();
			}  
		}  
		
		/**
		 * 获取Cookie值;
		 * */  
		public function get(key:String):Object{       
			if(!_FSO) return null;
			
			return contains(key)?_FSO.data.cookie[KeyPrefix+key]:null;  
		}  
		
		/**
		 * Cookie值是否存在
		 * */  
		public function contains(key:String):Boolean{  
			if(!_FSO) return false;
			
			key = KeyPrefix+ key;   
			return _FSO.data.cookie != undefined && _FSO.data.cookie[key] != undefined;  
		}  
		
		public function GetKeys():String{
			if(!_FSO) return '';
			var R:String='';
			for(var Prop:String in _FSO.data.cookie){
				if(Prop.search(KeyPrefix)>-1)
					R+=Prop.replace(KeyPrefix,'')+'|';
			}
			if(R.length>0)
				R=R.slice(0,R.length-1);
			return R;
		}
	}  
}